<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主题列表</title>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" th:href="@{/css/beehive-admin.css}">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}">
    <script th:src="@{/js/jquery-3.4.0.js}"></script>
    <script th:src="@{/js/bootstrap.js}"></script>
    <script th:src="@{/layui/layui.js}"></script>

    <script>
        function toModify(id, name) {
            $("#themeId").val(id);
            $("#rename").val(name);
            $("#modifyModal").modal('show');
        }
    </script>

</head>
<body>

<div class="container-fluid">

    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a th:href="@{/admin}" style="text-decoration: none">首页</a></li>
            <li class="breadcrumb-item"><a href="#" style="text-decoration: none">图书管理</a></li>
            <li class="breadcrumb-item active" aria-current="page">主题列表</li>
        </ol>
    </nav>

    <div class="theme-div">
        <span>T_THEME</span>
        <span class="theme-span">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
              添加一条新主题！
            </button>
        </span>

        <!-- Modal 添加记录 -->
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
             aria-hidden="true">
            <div class="modal-dialog" role="document">
                <form method="post" th:action="@{/theme/add}">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">添加新记录</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <div class="form-inline ">
                                <label for="name">名称：</label>
                                <input type="text" id="name" name="name" class="form-control bee-input" placeholder="2-4个字" maxlength="4">
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                            <button type="submit" class="btn btn-primary">提交</button>
                        </div>
                    </div>

                </form>
            </div>
        </div>


    </div>
    <hr/>

    <table class="table table-hover">
        <thead class="thead-dark" style="text-align: center">
        <tr>
            <th scope="col">ID</th>
            <th scope="col">主题名称</th>
            <th scope="col">图书数量</th>
            <th scope="col">访问次数</th>
            <th scope="col">下载次数</th>
            <th scope="col">操作</th>
        </tr>
        </thead>
        <tbody style="text-align: center">
        <tr th:each="theme : ${themes.records}" th:id="|theme${theme.id}|">
            <td>[[${theme.id}]]</td>
            <td>[[${theme.name}]]</td>
            <td>[[${theme.bookNum == null ? 0 : theme.bookNum}]]</td>
            <td>[[${theme.visitedCount == null ? 0 : theme.visitedCount}]]</td>
            <td>[[${theme.downloadCount == null ? 0 : theme.downloadCount}]]</td>
            <td>
                <button type="button" class="btn btn-info" th:onclick="toModify([[${theme.id}]], [[${theme.name}]])">修改</button>
                <button class="btn btn-danger" th:onclick="|toDelete(${theme.bookNum},${theme.id})|">删除</button>
            </td>

            <div class="modal fade" id="modifyModal">
                <div class="modal-dialog">
                    <form method="post" th:action="@{/theme/put}">
                        <input type="hidden" id="themeId" name="id">
                        <input type="hidden" name="_method" value="put">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="modifyModalLabel">修改</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <div class="form-inline ">
                                    <label for="rename">名称：</label>
                                    <input type="text" id="rename" name="name" class="form-control bee-input" maxlength="4">
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </tr>
        </tbody>
    </table>
    <hr/>

    <div>
        <nav style="display: inline-block">
            <ul class="pagination">
                <li th:class="${1 == themes.current ? 'page-item disabled' : 'page-item'}">
                    <!-- thymeleaf link传递参数 -->
                    <a class="page-link" th:href="@{/admin/themeList(pn=${themes.current} - 1)}">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <!-- thymeleaf 表达式语法 -->
                <li th:class="${num == themes.current?'page-item active':'page-item'}"
                    th:each="num : ${#numbers.sequence(themes.current, themes.current + 2)}">
                    <a class="page-link" th:href="@{/admin/themeList(pn=${num})}">[[${num}]]</a>
                </li>
                <li th:class="${themes.pages == themes.current ? 'page-item disabled' : 'page-item'}">
                    <a class="page-link" th:href="@{/admin/themeList(pn=${themes.current} + 1)}">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </ul>
        </nav>

        <span style="float:right; color: #ff8c94">
            当前第 [[${themes.current}]] 页 总计 [[${themes.pages}]] 页 共 [[${themes.total}]] 条
        </span>

    </div>
</div>

<script th:inline="javascript">

    function toDelete(bookNum, id) {

        if (bookNum == null) {
            $.ajax({
                url: '/theme/delete/' + id,
                type: 'post',
                success: function (res) {
                    if (res) {
                        layer.open({title: '提示', icon: 1, content: '删除成功！', shadeClose: true, time: 1000});
                        // 删除所在行，延迟0.8s执行
                        setTimeout(function () {$("#theme" + id).remove();}, 1000);
                    } else {
                        layer.open({title: '提示', icon: 0, content: '删除失败！', shadeClose: true, time: 1000});
                    }
                },
                error: function () {
                    layer.open({title: '提示', icon: 2, content: 'Beehive开小差去啦！', shadeClose: true, time: 1000});
                }
            });
        } else {
            layer.open({title: '警告', icon: 0,
                content: '该主题有<span style="color: red">' + bookNum + "</span>本书，不允许删除！",
                shadeClose: true, time: 3000});
        }
    }

    layui.use('layer', function () {
        let layer = layui.layer;
    });


</script>


</body>
</html>